﻿<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
    protected string Notice = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //权限管理子程序==========================================================
            string opp3 = (string)Session["OPP3"];
            if (opp3 == null || opp3 == ",")
            {
                this.LeiBie.DataBind();
            }
            else
            {
                if (opp3.StartsWith(","))
                {
                    opp3 = opp3.Substring(1);
                }
                if (opp3.EndsWith(","))
                {
                    opp3 = opp3.Substring(0, opp3.Length - 1);
                }
                this.DS_LeiBie.SelectCommand = "select ID, [品名]+' '+[钢级]+' '+[规格] AS 名称 FROM 原料 where [ID] in (" + opp3 + ")";
            }
            this.LeiBie.DataBind();
            //权限管理子程序==========================================================
            this.Date2.Text = DateTime.Today.ToLongDateString();
        }
    }
    protected string GetState()
    {
        string state = " where 原料ID=" + this.LeiBie.Text;

        if (this.FenChang.Text != "0")
        {
            state += " and 分厂=" + this.FenChang.Text;
        }

        if (this.GongHuo.Text != "0")
        {
            state += " and 供货ID=" + this.GongHuo.Text;
        }
        return state;
    }
    protected void Button_Query_Click(object sender, EventArgs e)
    {
        if (this.LeiBie.Text == "0")
        {
            this.Notice = "请选择您要统计的原料类别";
            return;
        }
        
        DateTime d;
        string d1, d2;
        d1 = d2 = "";
        if (DateTime.TryParse(this.Date1.Text, out d))
        {
            d1 = d.ToShortDateString() ;
        }
        if (DateTime.TryParse(this.Date2.Text, out d))
        {
            d2 = d.ToShortDateString();
        }
        
        string state = this.GetState();
        string sql = "select @收入重量=SUM(磅重),@收入支数=SUM(支数) from 原料到货记录" + state;
        if (d2 != "")
        {
            sql += " and 日期<='" + d2 + "'";
        }
        sql += ";\nselect @发出重量=SUM(重量),@发出支数=SUM(支数) from 原料发货记录" + state;
        if (d2 != "")
        {
            sql += " and 日期<='" + d2 + "'";
        }
        sql += "\nif @收入重量 is null begin set @收入重量=0 end"
            + "\nif @收入支数 is null begin set @收入支数=0 end"
            + "\nif @发出重量 is null begin set @发出重量=0 end"
            + "\nif @发出支数 is null begin set @发出支数=0 end";
        sql += ";\nset @库存重量= @收入重量-@发出重量";
        sql += ";\nset @库存支数= @收入支数-@发出支数";

        sql += ";\nselect @收入重量=SUM(磅重),@收入支数=SUM(支数) from 原料到货记录" + state;
        if (d1 != "")
        {
            sql += " and 日期>='" + d1+"'";
        }
        if (d2 != "")
        {
            sql += " and 日期<='" + d2 + "'";
        }
        sql += ";\nselect @发出重量=SUM(重量),@发出支数=SUM(支数) from 原料发货记录" + state;
        if (d1 != "")
        {
            sql += " and 日期>='" + d1 + "'";
        }
        if (d2 != "")
        {
            sql += " and 日期<='" + d2 + "'";
        }
        sql += "\nif @收入重量 is null begin set @收入重量=0 end"
            + "\nif @收入支数 is null begin set @收入支数=0 end"
            + "\nif @发出重量 is null begin set @发出重量=0 end"
            + "\nif @发出支数 is null begin set @发出支数=0 end";
        //Response.Write(sql);
        //Response.End();
        this.SqlDataSource1.SelectCommand = sql;
        this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        
    }

    protected void Button_Reset_Click(object sender, EventArgs e)
    {

        this.GongHuo.Text = "0";
        this.FenChang.Text = "0";

        this.Date1.Text = "";
        this.Date2.Text = "";

    }

    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        this.Label_Result.Text = "<span style='display:inline-block;width:200px;'>到货重量：" + (double)e.Command.Parameters[0].Value
            + " 吨</span><span style='display:inline-block;width:200px;'>总支数：" + (int)e.Command.Parameters[1].Value
            + " 支</span><br>";
        this.Label_Result.Text += "<span style='display:inline-block;width:200px;'>发货重量：" + (double)e.Command.Parameters[2].Value
            + " 吨</span><span style='display:inline-block;width:200px;'>总支数：" + (int)e.Command.Parameters[3].Value
            + " 支</span><br>";
        this.Label_Result.Text += "<span style='display:inline-block;width:200px;'>未发重量：" + (double)e.Command.Parameters[4].Value
            + " 吨</span><span style='display:inline-block;width:200px;'>总支数：" + (int)e.Command.Parameters[5].Value
            + " 支</span><br>";
    }
</script>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript" src="/common.js"></script>
<form id="form1" runat="server">
<div id="wintop" class="wintop">原料收发存统计</div>
<asp:Panel ID="Panel1" runat="server" GroupingText="查询统计" CssClass="panel">

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SCMConnectionString1 %>" OnSelected="SqlDataSource1_Selected" >
    <SelectParameters>
        <asp:Parameter Name="收入重量" DbType="double" Size="9" Direction="Output" />
        <asp:Parameter Name="收入支数" DbType="Int32" Size="4" Direction="Output" />
        <asp:Parameter Name="发出重量" DbType="double" Size="9" Direction="Output" />
        <asp:Parameter Name="发出支数" DbType="Int32" Size="4" Direction="Output" />
        <asp:Parameter Name="库存重量" DbType="double" Size="9" Direction="Output" />
        <asp:Parameter Name="库存支数" DbType="Int32" Size="4" Direction="Output" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SCMConnectionString1 %>" 
    SelectCommand="SELECT [ID], 名称 FROM 原料供货单位 union select 0 as [ID] , '全部' as 名称">
    </asp:SqlDataSource>
<!-- =================================================================================================== -->
<!-- ========                               原料类别列表                                     =========== -->
<!-- ========                                                                               =========== -->
<!-- =================================================================================================== -->
<asp:SqlDataSource ID="DS_LeiBie" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:SCMConnectionString1 %>" 
                    SelectCommand="SELECT [ID],[品名]+' '+[钢级]+' '+[规格] AS 名称 FROM [原料]" >
</asp:SqlDataSource>
原料：
<asp:DropDownList ID="LeiBie" runat="server" Width="250px" Font-Size="12px" DataSourceID="DS_LeiBie" 
                               DataTextField="名称" DataValueField="ID">
</asp:DropDownList>
<!-- =================================================================================================== -->



<span style="margin-left:10px;"></span>使用单位：<asp:DropDownList ID="FenChang" Width="120px"
    runat="server">
    <asp:ListItem Text="全部" Value="0"></asp:ListItem>
    <asp:ListItem Text="一分厂" Value="1"></asp:ListItem>
    <asp:ListItem Text="二分厂" Value="2"></asp:ListItem>
    <asp:ListItem Text="三分厂" Value="3"></asp:ListItem>
    <asp:ListItem Text="四分厂" Value="4"></asp:ListItem>
    <asp:ListItem Text="五分厂" Value="5"></asp:ListItem>
</asp:DropDownList>

<span style="margin-left:10px;"></span>供货单位：<asp:DropDownList 
    ID="GongHuo" runat="server" DataSourceID="SqlDataSource2"  Width="120px"
    DataTextField="名称" DataValueField="ID"></asp:DropDownList>
        
<div style="font-size:6px;height:6px;line-height:6px;"></div>
起始日期：<asp:TextBox ID="Date1" runat="server" CssClass="textbox" Width="105px"></asp:TextBox><img alt="" src="/Icons/29.png" align="middle" onclick="makecalendar(document.getElementById('Date1').value, document.getElementById('Date1'));" />
<span style="margin-left:10px;"></span>截止日期：<asp:TextBox ID="Date2" runat="server" CssClass="textbox" Width="105px"></asp:TextBox><img alt="" src="/Icons/29.png" align="middle" onclick="makecalendar(document.getElementById('Date2').value, document.getElementById('Date2'));" />

<span style="margin-left:10px;"></span><asp:Button ID="Button_Query" runat="server" Text="统 计" UseSubmitBehavior="false" 
        CssClass="mbutton" onclick="Button_Query_Click" />
<asp:Button ID="Button_Reset" runat="server" Text="清空条件" UseSubmitBehavior="false" 
        CssClass="mbutton" onclick="Button_Reset_Click" />
        
        
    <asp:Panel ID="Panel2" runat="server" CssClass="panel">
        <asp:Label ID="Label_Result" runat="server" Text="请点击“统计”按钮进行统计" ForeColor="Red"></asp:Label>
    </asp:Panel>
    
</asp:Panel>

</form>
<script type="text/javascript">
    var notice = '<%=this.Notice %>';
    if (notice != '') {
        parent.showmsgbox(notice);
    }
</script>
<script type="text/javascript">

</script>
</body>
</html>
